import {Component, Vue} from 'vue-property-decorator';
import {ElForm} from "element-ui/types/form";
import $http from "@/core/utils/http";

/**
 * 新版本
 * 顶层处理类
 */
@Component({
    name: 'BaseVue',
})
export default class BaseVue extends Vue {
    //表格复选框事件
    selectedRows: any = [];
    selectedRowKeys: any = [];

    selectionChange(val: any) {
        this.selectedRows = val;
        val.filter((it: any) => {
            this.selectedRowKeys.push(it['id'])
        });
        console.log("selectedRows", this.selectedRows);
        console.log("selectedRowKeys", this.selectedRowKeys);
    }

    //表单数据
    public nowUpdateData: any = {};
    public nowUpdateDataIndex: any = 0;
    public tinymceActive: boolean = false;

    //表单数据
    public mFormData: any = {};

    //表格数据
    public tableData: any = [];

    //加载框
    public tableLoading: boolean = false;
    //查询条件
    public queryParam: any = {
        size: 10,
        current: 1
    };

    //左侧详情抽屉
    public drawerInfo: any = {
        title: "",
        type: "info",
        size: 60,
        index: 0,
        direction: "ltr",
        isOpen: false,
        data: {},
        row: {},
    };


    //消息对话框
    public dialogMsg: any = {
        id: "",
        ids: [],
        title: "消息通知",
        content: "是否执行当前操作？",
        isOpen: false,
        open: false,
        type: "",
        param: {}//参数
    };

    //弹出层数据
    public dialogData: any = {
        id: "",
        ids: [],
        isOpen: false,
        isOpen1: false,
        title: "",
        type: "",
    };

    /**
     * 执行查询分页
     */
    public pageData(url: any, page: any, config: any = null): any {
        this.tableLoading = true;
        if (page) {
            this.queryParam.size = page.pageSize;
            this.queryParam.current = page.currentPage;
        }
        return $http.post(url, this.queryParam, config)
            .then((res: any) => {
                if (res.success) {
                    this.tableData = res.data['records'];
                    //全部删除后，跳转到上一页
                    if (this.tableData.length == 0 && this.queryParam.current > 0) {
                        this.queryParam.current = ((this.queryParam.current * 1) - 1);
                        this.pageData(url, null, config);
                        return res;
                    }
                    //更新分页
                    this.queryParam.size = res.data['size'];
                    this.queryParam.current = res.data['current'];
                    this.queryParam.currentSize = res.data['records'].length;
                    this.queryParam.pages = res.data['pages'];
                    this.queryParam.total = res.data['total'];
                }
                this.tableLoading = false;
                return res;
            });
    }


    /**
     * 展示数据
     * @param title 标题
     * @param type 类型
     * @param index 数据位置
     * @param row 展示数据内容
     */
    public baseShowDataDialog(title: any = "详情", type: any = "info", index: any = 0, row: any = {}) {
        this.mFormData = JSON.parse(JSON.stringify(row));
        this.dialogData.title = title;
        this.dialogData.type = type;
        this.dialogData.index = index;
        this.dialogData.isOpen = true;
        this.nowUpdateData = JSON.parse(JSON.stringify(row));
        this.baseCleanValidate();

    }

    /**
     * 左侧抽屉-展示数据
     * @param title 标题
     * @param index 数据位置
     * @param row 展示数据内容
     */
    public baseShowDrawerInfo(title: any = "详情", index: any = 0, row: any = {}) {
        this.drawerInfo.title = title;
        this.drawerInfo.index = index;
        this.drawerInfo.row = row;
        this.drawerInfo.isOpen = true;
        this.drawerInfo.data = JSON.parse(JSON.stringify(row));
    }

    /**
     * 还原修改数据
     */
    public baseCancelUpdate() {
        this.mFormData = JSON.parse(JSON.stringify(this.nowUpdateData));
        this.baseCleanValidate()
    }

    /**
     * 清除表单验证
     */
    public baseCleanValidate() {
        if ((this.$refs.mFormData as ElForm)) {
            (this.$refs.mFormData as ElForm).clearValidate();
        }
        setTimeout(() => {
            if ((this.$refs.mFormData as ElForm)) {
                (this.$refs.mFormData as ElForm).clearValidate();
            }
        }, 200);
    }

    public msgBox(title: any = "通知", message: any = "是否执行操作？",
                  funOk: any = () => {
                  },
                  funCancel: any = () => {
                  },
                  type: any = "warning", btnOkText: any = "确定", btnCancelText: any = "取消") {
        this.$confirm(message, title, {
            confirmButtonText: btnOkText,
            cancelButtonText: btnCancelText,
            dangerouslyUseHTMLString: true,
            type: type,
        }).then(() => {
            funOk();
        }).catch(() => {
            funCancel();
        });
    }

    public msgBoxInput(title: any = "通知", message: any = "是否执行操作？",
                       funOk: any = (value: any) => {
                       },
                       funCancel: any = () => {
                       },
                       type: any = "warning", btnOkText: any = "确定", btnCancelText: any = "取消") {
        this.$prompt(message, title, {
            confirmButtonText: btnOkText,
            cancelButtonText: btnCancelText,
        }).then((val: any) => {
            funOk(val.value);
        }).catch(() => {
            funCancel();
        });
    }


    /**
     * 重置查询条件
     */
    public baseResetQuery() {
        let parse = JSON.parse(JSON.stringify(this.queryParam));
        this.queryParam = {
            pages: parse.pages,
            total: parse.total,
            current: parse.current,
            size: parse.size,
        };
    }

}
